package cn.hefan.web.servlet.user;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hefan.domain.Pages;
import cn.hefan.domain.User;
import cn.hefan.service.findUserService;

public class findUserServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String divide = request.getParameter("divide");//分组
		String pageSizeStr = request.getParameter("pageSize");//每页展示数
		String currentPageStr = request.getParameter("currentPage");//当前页
		int currentPage = 0;
		if(currentPageStr==null||currentPageStr.length()==0) {
			currentPage = 1;
		}else {
			currentPage = Integer.parseInt(currentPageStr);
		}
		int pageSize = 0;
		if(pageSizeStr==null||pageSizeStr.length()==0) {
			pageSize = 10;
		}else {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		Pages<User> page = null;
		if(divide==null) {
			page =  findUserService.findAll(currentPage,pageSize);
		}else {
			page = findUserService.findDivide(divide,currentPage,pageSize);
		}
		//发送数据给前端
		ObjectMapper mapper = new ObjectMapper();
		response.setContentType("application/json;charset=utf-8");
		mapper.writeValue(response.getOutputStream(),page);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
